package com.water.devtool.ui.define;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.water.devtool.define.LevelConfig;
import com.water.devtool.define.ProjectBean;
import com.water.devtool.ui.UiUtil;

public class LevelConfigComposite extends Composite {
	final static Logger log = LoggerFactory.getLogger(LevelConfigComposite.class);
	private ProjectBean project;
	private Table table;
	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public LevelConfigComposite(Composite parent, int style) {
		super(parent, style);

		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				closeTab();
			}
		});
		button.setBounds(862, 448, 98, 30);
		button.setText("关闭");

		Button button_1 = formToolkit.createButton(this, "确定", SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int count = table.getItemCount();
				Map<String, LevelConfig> levelConfig = new HashMap<String, LevelConfig>();
				for (int i = 0; i < count; i++) {
					TableItem item = table.getItem(i);
					LevelConfig config = new LevelConfig();
					config.setOrderNo(Integer.parseInt(item.getText(0)));
					config.setLevel(item.getText(1));
					config.setName(item.getText(2));
					config.setFileFormat(item.getText(3));
					config.setRootPath(item.getText(4));
					config.setPathStyle(item.getText(5));
					config.setTemplateFile(item.getText(6));
					config.setFileName(item.getText(7));
					if (StringUtils.isBlank(item.getText(8))) {
						config.setLastPack(config.getLevel());
					} else {
						config.setLastPack(item.getText(8));
					}

					levelConfig.put(config.getLevel(), config);
				}

				project.setLevelConfig(levelConfig);

			}
		});
		button_1.setBounds(622, 448, 98, 30);

		Button button_2 = new Button(this, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				deleteItem();
			}
		});
		button_2.setBounds(142, 448, 98, 30);
		formToolkit.adapt(button_2, true, true);
		button_2.setText("删除");

		Button button_3 = new Button(this, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				project.setLevelConfig(LevelConfig.defaultConfig());
				clearData();
			}
		});
		button_3.setText("默认");
		button_3.setBounds(382, 448, 98, 30);
		formToolkit.adapt(button_3, true, true);

	}

	protected void deleteItem() {
		int i = table.getSelectionIndex();
		table.remove(i);
	}

	protected void clearData() {
		project.setLevelConfig(LevelConfig.defaultConfig());

		table.removeAll();
		setTableData();
	}

	private void setTableData() {
		List<LevelConfig> list = project.listLevelConfig();
		for (LevelConfig config : list) {
			TableItem item = new TableItem(table, SWT.NONE);
			String[] param = new String[] { String.valueOf(config.getOrderNo()), config.getLevel(), config.getName(),
					config.getFileFormat(), config.getRootPath(), config.getPathStyle(), config.getTemplateFile(),
					config.getFileName(), config.getLastPack() == null ? config.getLevel() : config.getLastPack() };
			item.setText(param);
		}

	}

	// 创建表格
	private void createTable() {
		// 表格布局
		GridData gridData = new org.eclipse.swt.layout.GridData();
		gridData.horizontalAlignment = SWT.LEFT;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalAlignment = SWT.FILL;

		// 创建表格，使用SWT.FULL_SELECTION样式，可同时选中一行
		table = new Table(this, SWT.FULL_SELECTION);
		table.setBounds(0, 0, 1103, 406);
		table.setHeaderVisible(true);// 设置显示表头
		table.setLinesVisible(true);// 设置显示表格线/*
		// 创建表头的字符串数组
		String[] tableHeader = { "序号", "层", "后缀", "文件格式", "根目录", "目录风格", "模板文件", "文件名", "包" };
		int[] widths = { 60, 100, 100, 100, 100, 300, 100, 100, 100 };
		for (int i = 0; i < tableHeader.length; i++) {
			TableColumn tableColumn = new TableColumn(table, SWT.NONE);
			tableColumn.setText(tableHeader[i]);
			// 设置表头可移动，默认为false
			tableColumn.setMoveable(false);
			tableColumn.setWidth(widths[i]);
		}

		setTableData();
		UiUtil.setTableEditor(table, tableHeader);

		// ******************************************************/
	}

	public void closeTab() {
		TabFolder tabFolder = (TabFolder) getParent();
		TabItem[] items = tabFolder.getSelection();
		for (TabItem tabItem : items) {
			tabItem.dispose();
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public void setProject(ProjectBean project) {
		this.project = project;
		createTable();
	}
}
